package com.scau.bookstore.service.impl;

import com.scau.bookstore.contract.service.AddressService;
import com.scau.bookstore.common.pojo.Address;
import com.scau.bookstore.repository.mapper.AddressDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.Predicate;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;


@Service
public class AddressServiceImpl implements AddressService {

    @Autowired
    private AddressDao addressDao;

    /**
     * 返回一个地址
     *
     * @return
     */
    public Address findByUserId(Long userId) {
        return addressDao.findByUserId(userId).get(0);
    }

    /**
     * 查询全部列表
     *
     * @return
     */
    public List<Address> findAll() {
        return addressDao.findAll();
    }


    /**
     * 条件查询+分页
     *
     * @param whereMap
     * @param page
     * @param size
     * @return
     */
    public Page<Address> findSearch(Map whereMap, int page, int size) {
        Specification<Address> specification = createSpecification(whereMap);
        PageRequest pageRequest = PageRequest.of(page - 1, size);
        return addressDao.findAll(specification, pageRequest);
    }


    /**
     * 条件查询
     *
     * @param whereMap
     * @return
     */
    public List<Address> findSearch(Map whereMap) {
        Specification<Address> specification = createSpecification(whereMap);
        return addressDao.findAll(specification);
    }

    /**
     * 根据ID查询实体
     *
     * @param id
     * @return
     */
    public Address findById(Long id) {
        return addressDao.findById(id).get();
    }

    /**
     * 增加
     *
     * @param address
     */
    public void add(Address address) {
        addressDao.save(address);
    }

    /**
     * 修改
     *
     * @param address
     */
    public void update(Address address) {
        addressDao.save(address);
    }

    /**
     * 删除
     *
     * @param id
     */
    public void deleteById(Long id) {
        addressDao.deleteById(id);
    }

    /**
     * 动态条件构建
     *
     * @param searchMap
     * @return
     */
    private Specification<Address> createSpecification(Map searchMap) {

        return (root, query, cb) -> {
            List<Predicate> predicateList = new ArrayList<>();
            // 收获地址
            if (searchMap.get("address") != null && !"".equals(searchMap.get("address"))) {
                predicateList.add(cb.like(root.get("address").as(String.class), "%" + searchMap.get("address") + "%"));
            }
            // 手机号码
            if (searchMap.get("phone") != null && !"".equals(searchMap.get("phone"))) {
                predicateList.add(cb.like(root.get("phone").as(String.class), "%" + searchMap.get("phone") + "%"));
            }
            // 收货人
            if (searchMap.get("receiveName") != null && !"".equals(searchMap.get("receiveName"))) {
                predicateList.add(cb.like(root.get("receiveName").as(String.class), "%" + searchMap.get("receiveName") + "%"));
            }

            return cb.and(predicateList.toArray(new Predicate[predicateList.size()]));

        };

    }

}
